﻿using System;
using System.Collections.Generic;
using Facebook;

namespace QuizzMe.Helpers
{
    /// <summary>
    /// Helper for facebook authentification handling
    /// </summary>
    public class Fb
    {
        private readonly FacebookClient _fb;
        private readonly Queue<EventHandler<FacebookApiEventArgs>> _eventHandlers;

        public Fb(FacebookClient fb)
        {
            _eventHandlers = new Queue<EventHandler<FacebookApiEventArgs>>();

            _fb = fb;
            _fb.GetCompleted += FireGetCompleted;
        }

        /// <summary>
        /// Add a LoginCompleted handler
        /// </summary>
        /// <param name="handler"></param>
        public void AddHandler(EventHandler<FacebookApiEventArgs> handler)
        {
            _eventHandlers.Enqueue(handler);
        }

        private void FireGetCompleted(object sender, FacebookApiEventArgs e)
        {
            if (_eventHandlers.Count > 0)
            {
                EventHandler<FacebookApiEventArgs> handler = _eventHandlers.Dequeue();
                handler(sender, e);
            }
        }

        public void QueryAsync(string format)
        {
           _fb.QueryAsync(format);
        }

        public void PostAsync(string meFeed, Dictionary<string, object> parameters)
        {
            _fb.PostAsync(meFeed, parameters);
        }

        /// <summary>
        /// Get a facebook user
        /// </summary>
        public void GetAsync(string meFeed, Dictionary<string, object> parameters = null)
        {
            if (parameters == null)
                _fb.GetAsync(meFeed, parameters);
            else
                _fb.GetAsync(meFeed);
        }
    }
}